<?php
namespace backend\models;

/**
 * 帮助中心
 *
 * @author huipeng.teng@liketry.com
 */
class HelpCenter extends \common\models\HelpCenter {
    public function search() {
        $query = self::find();

        $dataProvider = new \yii\data\ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 50,
            ],
            'sort' => [
                'defaultOrder' => [
                    'update_time' => SORT_DESC
                ]
            ],
        ]);

        return $dataProvider;
    }

    /**
     * 添加信息
     * @param string $title 标题
     * @param string $content 内容
     * @param int $is_show 是否显示
     * @return array
     */
    public function addRow($title, $content, $is_show) {
        $return = ['flag' => false, 'msg' => ''];
        if(empty($title)) {
            $return['msg'] = '请输入标题';
        } elseif(empty($content)) {
            $return['msg'] = '请输入内容';
        }

        if($return['msg']) {
            return $return;
        }

        $dataArray = array();
        $dataArray['title'] = $title;
        $dataArray['content'] = $content;
        $dataArray['is_show'] = $is_show;
        $dataArray['create_time'] = date('Y-m-d H:i:s');
        $dataArray['update_time'] = date('Y-m-d H:i:s');
        self::getDb()->createCommand()
                ->insert(self::tableName(), $dataArray)
                ->execute();

        if(self::getDb()->lastInsertID > 0) {
            $return['flag'] = true;
            $return['msg'] = '添加成功';
        } else {
            $return['msg'] = '添加失败';
        }

        return $return;
    }

    /**
     * 更新信息
     * @param int $id 帮助中心ID
     * @param string $title 标题
     * @param string $content 内容
     * @param int $is_show 是否显示
     * @return array
     */
    public function updateRow($id, $title, $content, $is_show) {
        $return = ['flag' => false, 'msg' => ''];
        if($id < 1) {
            $return['msg'] = 'ID不能小于1';
        } elseif(empty($title)) {
            $return['msg'] = '请输入标题';
        } elseif(empty($content)) {
            $return['msg'] = '请输入内容';
        }

        if($return['msg']) {
            return $return;
        }

        $row = self::findOne($id);
        if(empty($row)) {
            $return['msg'] = '记录为空';
            return $return;
        }

        $dataArray = array();
        $dataArray['title'] = $title;
        $dataArray['content'] = $content;
        $dataArray['is_show'] = $is_show;
        $dataArray['update_time'] = date('Y-m-d H:i:s');
        $affected_rows = self::getDb()->createCommand()
                ->update(self::tableName(), $dataArray, 'help_center_id=:id', [':id' => $id])
                ->execute();

        if($affected_rows > 0) {
            $return['flag'] = true;
            $return['msg'] = '更新成功';
        } else {
            $return['msg'] = '更新失败';
        }

        // 删除Redis缓存
        \services\models\HelpCenter::deleteByIdFromRedis($id);

        return $return;
    }
}
